// pages/user/user.js
var util=require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bck:"../../static/img/imgs/boy-head.png",
    name:"",
    openid:"",
    time:"",
    token: "", // 用户登录之后的token
    tipsIsShow: true, // 提示信息是否显示
    showList:[],
    one:"",
    t:"",
    s:"",
    f:"",
    v:""
  },

  ord_1:function(e){
    wx.navigateTo({
      url: '../order/order?id='+e.currentTarget.dataset.id,
     })
  },
  perdata:function(){
    let that = this
    if(that.data.bck=="../../static/img/imgs/boy-head.png"){
      return
    }else{
      wx.navigateTo({
        url: '../personalInfo/personalInfo',
       })
    }
    
  },
  reg(e){
    let that = this
    // wx.getSetting({
    //   success (res) {
    //     console.log(res.authSetting)
    //     if(res.authSetting['scope.userInfo']){
    //       console.log(666)
    //         that.data.time=util.formatTime(new Date());
           
    //     }
    //   }
    // })
    // that.data.time=util.formatTime(new Date());

   
    wx.login({
      success (res) {
        console.log(res.code)
        if (res.code) {
          //发起网络请求
          wx.request({//登录换openid
            url: 'http://api_devs.wanxikeji.cn/api/codeExchangeOpenid',
            method:'POST',
            data: {
              code:res.code
            },
            success: res => { 
              that.openid=res.data.data.openid
              console.log(res)
              wx.request({//openid换取token
                url: 'http://api_devs.wanxikeji.cn/api/refreshToken',
                method:'POST',
                data: {
                  openid:res.data.data.openid,
                },
                success: res => { 
                  console.log(res)
                  if(res.data.msg==""){
                    wx.setStorage({
                      key:"token",
                      data:res.data.data.token
                    })
                    wx.setStorage({
                      key:"name",
                      data:e.detail.userInfo.nickName
                    })
                    wx.setStorage({
                      key:"bck",
                      data:e.detail.userInfo.avatarUrl
                    })
                    that.setData({
                      bck:e.detail.userInfo.avatarUrl,
                      name:e.detail.userInfo.nickName
                    })
                  }else{
                    wx.showToast({
                      title: '没注册呢傻逼',
                      icon: 'success',
                      duration: 1500
                    })
                  }
                  that.init()
                }
              })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
   
  
    

  },

  login(){
    let that=this
    wx.login({
      success (res) {
        if (res.code) {
          //发起网络请求
          console.log(res.code)
          wx.request({//换openid

            url: 'http://api_devs.wanxikeji.cn/api/codeExchangeOpenid',
            method:'POST',
            data: {
              code: res.code
            },
            success: res => { 
              that.openid=res.data.data.openid
              console.log(res)
              console.log("openid为：", that.openid);
              wx.request({//注册接口
                url: 'http://api_devs.wanxikeji.cn/api/register',
                method:'POST',
                data: {
                  name:1,
                  pw:1,
                  openid:res.data.data.openid,
                  nick_name:that.name,
                  icon:that.bck
                },
                success: res => { 
                  console.log("用户信息为：", res)
                  if(res.data.msg=="用户已存在"){
                    wx.showToast({
                      title: '用户已存在',
                      icon: 'success',
                      duration: 1500
                    })
                  }else{
                    wx.showToast({
                      title: '注册成功',
                      icon: 'success',
                      duration: 1500
                    })
                  }
                }
              })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  // 地址管理
  address(){
    // 获取token
    wx.getStorage({
      key: 'token',
      success: res => {
        console.log("用户token为：", res.data)
        this.data.token = res.data
        // 判断是否登录
        if (this.data.token) {
          this.regAddress()
        } 
      },
      fail: err => {
        console.log("获取token失败", err)
        this.setData({
          tipsIsShow: false
        })
        setTimeout( () => {
          this.setData({
            tipsIsShow: true
          })
        }, 1000)
      }
    })
  },

  // 地址列表验证（有则展示，无则新增）
  regAddress() {
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/userAddressList',
      data: {
        token: this.data.token
      },
      success: res => {
        console.log("用户地址列表为：", res.data)
        if (res.data.data.length) {
          wx.navigateTo({
            url: '../address/address?hasAddress=true',
          })
        } else {
          wx.navigateTo({
            url: '../address/address?hasAddress=false',
          })
        }
      },
      fail: err => {
        console.log("用户地址获取失败：", err)
      }
    })
  },
  search:function(){
    let that = this;
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/goodList',
      method:'POST',
      data:{
        page:"1",
        size:"10",
      },success:function(res){
         that.showList = res.data.data.data;
        that.setData({
          showList:that.showList,
        })
      }
    })

  },
  goods:function(e){
    let index = e.currentTarget.dataset.index;
    console.log(this.data.showList[index].good_id);
    wx.navigateTo({
      url: '../commodity/commodity?indexArr='+ this.data.showList[index].good_id
    })
  },
  init:function(){
    let that=this
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/orderList',
      method:'POST',
      data: {
        token:wx.getStorageSync('token'),
        // status:val
      },
      success(res){
        console.log(res.data.data.data)
        for(let i=0;i<res.data.data.data.length;i++){
          if(res.data.data.data[i].status==1){
            that.data.one++
            that.setData({
              one:that.data.one++
            })
          }
          if(res.data.data.data[i].status==2){
            that.data.t++
            that.setData({
              t:that.data.t++
            })
          }
          if(res.data.data.data[i].status==4){
            that.data.s++
            that.setData({
              s:that.data.s++
            })
          }
          if(res.data.data.data[i].status==5){
            that.data.f++
            that.setData({
              f:that.data.f++
            })
          }
          if(res.data.data.data[i].status==6){
            that.data.v++
            that.setData({
              v:that.data.v++
            })
          }
        }
        if(res.data.data.data!=undefined){
          that.setData({
            data:res.data.data.data
          })
        }else{
          that.setData({
            data:[]
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('token')!=""){
      this.setData({
        name:wx.getStorageSync('name'),
        bck:wx.getStorageSync('bck')
      })
    }
    this.search()
this.init()


   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.one="",
    this.data.t="",
    this.data.s="",
    this.data.f="",
    this.data.v="",
    this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})